let util = require('../../utils/util.js');
let api = require('../../config/api.js');
const pay = require('../../services/pay.js');
const app = getApp()

Page({
    data: {
        checkedGoodsList: [],
        checkedAddress: {},
        goodsTotalPrice: 0.00, //商品总价
        freightPrice: 0.00, //快递费
        orderTotalPrice: 0.00, //订单总价
        actualPrice: 0.00, //实际需要支付的总价
        addressId: 0,
        goodsCount: 0,
        postscript: '',// 备注
        payMethodItems: [{
                name: 'offline',
                value: '线下支付'
            },
            {
                name: 'online',
                value: '在线支付',
                checked: 'true'
            },
        ],
        payMethod:1,
        sign: 0,
        cartId: '',
    },
    payChange(e){
        let val = e.detail.value;
        if(val == 'offline'){
            this.setData({
                payMethod:0
            })
        }
        else{
            this.setData({
                payMethod:1
            })
        }
    },
    toSelectAddress: function () {
        wx.navigateTo({
            url: '/pages/ucenter/address/index?type=1',
        });
    },
    toAddAddress: function () {
        wx.navigateTo({
            url: '/pages/ucenter/address-add/index',
        })
    },
    bindinputMemo(event) {
        let postscript = event.detail.value;
        this.setData({
            postscript: postscript
        });
    },
    onLoad: function (options) {
        let sign = options.sign;
        let cartId = options.cart_id;
        this.setData({
            sign: sign,
            cartId: cartId,
        })
    },
    onUnload: function () {
        wx.removeStorageSync('addressId');
    },
    onShow: function () {
        this.getCheckoutInfo();
    },
    onPullDownRefresh: function () {
        wx.showNavigationBarLoading()
        try {
            var addressId = wx.getStorageSync('addressId');
            if (addressId == 0 || addressId == '') {
                addressId = 0;
            }
            this.setData({
                'addressId': addressId
            });
        } catch (e) {
            // Do something when catch error
        }
        this.getCheckoutInfo();
        wx.hideNavigationBarLoading() //完成停止加载
        wx.stopPullDownRefresh() //停止下拉刷新
    },
    getCheckoutInfo: function () {
        let that = this;
        let cartId = that.data.cartId;
        let sign = that.data.sign;
        let token = wx.getStorageSync('token');
        let addressId = wx.getStorageSync('addressId');
        util.request(api.CartCheckout, {
            token: token,
            sign: sign,
            cart_id: cartId,
            address_id: addressId
        }, "POST").then(function (res) {
            if (res.code == 0) {
                that.setData({
                    checkedGoodsList: res.data.checked_goods,
                    checkedAddress: res.data.checked_address,
                    actualPrice: res.data.actual_price,
                    orderTotalPrice: res.data.order_price,
                    goodsCount: res.data.goods_count,
                    freightPrice: res.data.fee_price,
                    addressId: res.data.checked_address.id
                });
                if (!addressId) {
                    wx.setStorageSync('addressId', res.data.checked_address.id);
                }
            } else {
                util.showErrorToast(res.msg);
                setTimeout(function(){
                    wx.navigateBack();
                },3000)
            }
        });
    },
    submitOrder: function (e) {
        if (this.data.addressId <= 0) {
            util.showErrorToast('请选择收货地址');
            return false;
        }
        let addressId = this.data.addressId;
        let postscript = this.data.postscript;
        let freightPrice = this.data.freightPrice;
        let actualPrice = this.data.actualPrice;
        let cartId = this.data.cartId;
        let sign = this.data.sign;
        let token = wx.getStorageSync('token');
        wx.showLoading({
            title: '',
            mask:true
        });
        util.request(api.OrderSubmit, {
            address_id: addressId,
            postscript: postscript,
            freight_price: freightPrice,
            actual_price: actualPrice,
            cart_id: cartId,
            sign: sign,
            token: token,
        }, 'POST').then(res => {
            if (res.code == 0) {
                wx.removeStorageSync('orderId');
                wx.setStorageSync('addressId', 0);
                const orderId = res.data.order_id;
                pay.payOrder(parseInt(orderId)).then(res => {
                    wx.redirectTo({
                        url: '/pages/payResult/payResult?status=1&orderId=' + orderId
                    });
                }).catch(res => {
                    wx.redirectTo({
                        url: '/pages/payResult/payResult?status=0&orderId=' + orderId
                    });
                });
            } else {
                util.showErrorToast(res.msg);
            }
            wx.hideLoading()
        });
    },
    offlineOrder: function (e) {
        if (this.data.addressId <= 0) {
            util.showErrorToast('请选择收货地址');
            return false;
        }
        let addressId = this.data.addressId;
        let postscript = this.data.postscript;
        let freightPrice = this.data.freightPrice;
        let actualPrice = this.data.actualPrice;
        util.request(api.OrderSubmit, {
            addressId: addressId,
            postscript: postscript,
            freightPrice: freightPrice,
            actualPrice: actualPrice,
            offlinePay: 1
        }, 'POST').then(res => {
            if (res.errno === 0) {
                wx.removeStorageSync('orderId');
                wx.setStorageSync('addressId', 0);
                wx.redirectTo({
                    url: '/pages/payOffline/index?status=1',
                })
            } else {
                util.showErrorToast(res.errmsg);
                wx.redirectTo({
                    url: '/pages/payOffline/index?status=0',
                })
            }
        });
    }
})